<template>
  <common-header page-title="乡村振兴故事" />
  <div class="stories-container">
    <div class="page-header">
      <h1 class="page-title">乡村振兴故事</h1>
      <p class="page-subtitle">记录乡村发展，讲述振兴故事</p>
    </div>

    <el-row :gutter="24" justify="center">
      <el-col
          v-for="(story, index) in stories"
          :key="index"
          :xs="24"
          :sm="12"
          :md="8"
          :lg="6"
          class="story-card-col"
      >
        <el-card
            shadow="hover"
            class="story-card"
            :body-style="{ padding: '0' }"
            @click="handleStoryClick(story)"
            @mouseenter="hoverCard(index)"
            @mouseleave="unhoverCard(index)"
        >
          <!-- 图片区域 -->
          <div class="card-image-container">
            <img
                v-if="story.image"
                :src="story.image"
                :class="['card-image', { 'image-hover': cardStates[index]?.hover }]"
                alt="故事配图"
            />
            <div v-else class="placeholder-image"></div>
          </div>

          <!-- 内容区域 -->
          <div class="card-content">
            <div class="card-header">
              <h3 class="card-title">{{ story.title }}</h3>
              <div class="card-tag" v-if="story.tag">{{ story.tag }}</div>
            </div>

            <p class="card-excerpt">
              {{ truncate(story.content, 120) }}
            </p>

            <!-- 底部信息栏 -->
            <div class="card-footer">
              <div class="footer-item">
                <el-icon><Clock /></el-icon>
                <span>{{ formatDate(story.createTime) }}</span>
              </div>
              <div class="footer-item">
                <el-icon><View /></el-icon>
                <span>{{ story.viewCount }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import CommonHeader from '@/views/user/support/components/CommonHeader.vue'
import { useRouter } from 'vue-router'
import { Clock, View } from '@element-plus/icons-vue'

const router = useRouter()
const stories = ref([])
const cardStates = ref({})

// 鼠标悬停效果控制
const hoverCard = (index) => {
  cardStates.value = {
    ...cardStates.value,
    [index]: { hover: true }
  }
}
const unhoverCard = (index) => {
  cardStates.value = {
    ...cardStates.value,
    [index]: { hover: false }
  }
}

// 获取故事列表
const fetchStories = async () => {
  try {
    const res = await request.get(BASE_URL+'/api/v1/government/private/sel')
    stories.value = res.data.map(story => ({
      ...story,
      tag: story.tag || ['乡村振兴', '三农故事'][Math.floor(Math.random() * 2)]
    }))
  } catch (err) {
    console.error('获取故事失败:', err)
  }
}

// 格式化时间
const formatDate = (date) => {
  if (!date) return ''
  const d = new Date(date)
  return `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2,'0')}-${d.getDate().toString().padStart(2,'0')}`
}

// 截取文本长度
const truncate = (text, length) => {
  return text && text.length > length ? text.slice(0, length) + '...' : text || ''
}

// 点击故事
const handleStoryClick = async (story) => {
  try {
    await request.get(
        `${BASE_URL}/api/v1/government/public/increment-video-view-count`,
        { params: { videoId: story.id } }
    )
    router.push({ name: 'StoryDetail', params: { id: story.id } })
  } catch (err) {
    console.error('更新浏览次数失败:', err)
  }
}

onMounted(() => {
  fetchStories()
})
</script>

<style scoped>
.stories-container {
  padding: 40px 20px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 120px);
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

.page-title {
  font-size: 2.2rem;
  color: #2c3e50;
  margin-bottom: 12px;
  font-weight: 600;
  letter-spacing: 1px;
}

.page-subtitle {
  font-size: 1rem;
  color: #7f8c8d;
  margin: 0;
}

.story-card-col {
  padding: 0 12px;
  margin-bottom: 24px;
}

.story-card {
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  border: none;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.story-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1) !important;
}

.card-image-container {
  height: 180px;
  overflow: hidden;
  position: relative;
  background-color: #eaeaea;
}

.placeholder-image {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #e0e0e0 0%, #f5f5f5 100%);
}

.card-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.card-image.image-hover {
  transform: scale(1.05);
}

.card-content {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-header {
  margin-bottom: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 1.1rem;
  color: #2c3e50;
  margin: 0;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.card-tag {
  background-color: #e8f4ff;
  color: #3498db;
  font-size: 0.75rem;
  padding: 3px 8px;
  border-radius: 4px;
  margin-left: 10px;
}

.card-excerpt {
  font-size: 0.95rem;
  color: #555;
  line-height: 1.6;
  margin-bottom: 20px;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: #95a5a6;
  padding-top: 15px;
  margin-top: auto;
  border-top: 1px solid #eee;
}

.footer-item {
  display: flex;
  align-items: center;
}

.footer-item .el-icon {
  margin-right: 5px;
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .stories-container {
    padding: 30px 15px;
  }

  .page-title {
    font-size: 1.8rem;
  }

  .story-card-col {
    padding: 0 8px;
    margin-bottom: 16px;
  }

  .card-content {
    padding: 15px;
  }

  .card-excerpt {
    -webkit-line-clamp: 3;
    font-size: 0.9rem;
  }
}
</style>